function pv = pvvar(cf, rate, df)
%PVVAR  Present value of varying cash flow.
%   PVVAR returns the net present value of a cash flow given the periodic
%   interest rate.
%
%   PV = pvvar(CF, RATE)
%   PV = pvvar(CF, RATE, DF)
%
%   Optional Inputs: DF
%
%   Inputs:
%     CF - A vector of varying cash flows. Include the initial investment
%          as the initial cash flow value (a negative number).
%
%   RATE - Scalar or vector of periodic interest rate(s). Enter as a decimal
%          fraction.
%
%   Optional Inputs:
%     DF - A vector of serial date numbers or date strings on which the cash
%          flows occur. Specify DF when there are irregular (nonperiodic) cash
%          flows. The default assumes CF contains regular (periodic) cash flows.
%
%   Outputs:
%     PV - The net present value of a varying cash flow.
%
%   Example:
%      Suppose an initial investment of $10,000 is made. The following cash
%      flow represents the yearly income realized by the investment. The annual
%      discount rate is 8%.
%
%         Year 1    $2000
%         Year 2    $1500
%         Year 3	$3000
%         Year 4	$3800
%         Year 5	$5000
%
%      To calculate the net present value of the periodic cash flow do the
%      following (Note that the original investment payment is included as the
%      first cash flow value):
%
%         pv = pvvar([-10000 2000 1500 3000 3800 5000], .08)
%         pv =
%                1715.39
%
%      Suppose an investment of $10,000 returns the following series of cash
%      flows at a discount rate of 9%.
%
%         Cash Flow  Dates
%         ---------  -----------------
%         -10000	 January 12, 1987
%           2500	 February 14, 1988
%           2000	 March 3, 1988
%           3000	 June 14, 1988
%           4000	 December 1, 1988
%
%      The variables CF and DF are defined as follows:
%
%         cf = [-10000; 2500; 2000; 3000; 4000];
%         df = {'01/12/1987'
%               '02/14/1988'
%               '03/03/1988'
%               '06/14/1988'
%               '12/01/1988'};
%
%         pv = pvvar(cf, .09, df)
%         pv =
%                142.16
%
%   See also PVFIX, FVVAR, IRR, FVVAR, PAYUNI.

%   Copyright 1995-2006 The MathWorks, Inc.
%   $Revision: 1.6.2.3 $   $Date: 2006/06/16 20:10:26 $

if nargin < 2
    error('Finance:pvvar:tooFewInputs', 'Too few inputs.')
end

[rowcf, colcf] = size(cf);

if rowcf == 1
    cf = cf(:);
    colcf = 1;
end

if colcf > 1
    if length(rate) == 1
        rate = rate*ones(1, colcf);
    end
end

if nargin == 3
    if ischar(df) || iscell(df)
        df = datenum(df);
    end

    df = df(:);

    % Check input dims
    [ecode, errMsg] = checksiz([size(cf);size(df)], mfilename);
    if ecode
        error('Finance:pvvar:invalidInputDims', errMsg)
    end
end

pv = zeros(1, colcf);

for loop = 1:colcf
    if nargin < 3
        tf = 0:length(cf(:, loop))-1;

    else
        % Length of date
        n = length(df(:,loop));

        % Number years from df(1) to df(n)
        f = floor(yearfrac(df(1, loop), df(n, loop)));
        if f == 0;
            f = 1;
        end

        tf = f*(df(:, loop)-df(1, loop))/(datemnth(df(1, loop), 12*f, 0, 0)-df(1, loop));
        tf = tf(1:n);
    end

    % Present value
    pv(loop) = sum(cf(:,loop)./(1+rate(loop)).^tf(:));
end


% [EOF]
